package model;

import java.util.*;

public class GraphFactory {
	
	private static GraphFactory instance = null;
	HashMap<String, IGraphWriter> writers = null;
	HashMap<String, IGraphReader> readers = null;
	
	protected GraphFactory(){}
	
	public static GraphFactory getInstance(){
		
		if(instance == null) {
	         instance = new GraphFactory();
	      }
		return instance;
	}
	
	public IGraphReader createReader(String aType) {		
		
		IGraphReader reader = null;
		
		if (readers != null && readers.containsKey(aType)){
			reader = readers.get(aType); 
		}
		else{
			if(readers == null){
				readers = new HashMap<String, IGraphReader>();
			}
			try{
				Class<?> classDefinition = Class.forName("model.".concat(aType.concat("GraphReader")));
				reader = (IGraphReader)classDefinition.newInstance();
			}catch (InstantiationException e) {
				System.out.println(e);
			}catch (IllegalAccessException e) {
				System.out.println(e);
			}catch (ClassNotFoundException e) {
				System.out.println(e);
			}
			readers.put(aType, reader);
		}
		return reader;
	}

	public IGraphWriter createWriter(String aType) {
		
		IGraphWriter writer = null;
		if (writers != null && writers.containsKey(aType)){
			writer = writers.get(aType); 
		}
		else{
			if(writers == null){
				writers = new HashMap<String, IGraphWriter>();
			}
			try{
			Class<?> classDefinition = Class.forName("model.".concat(aType.concat("GraphWriter")));
			writer = (IGraphWriter)classDefinition.newInstance();
			
			}catch (InstantiationException e) {
				System.out.println(e);
			}catch (IllegalAccessException e) {
				System.out.println(e);
			}catch (ClassNotFoundException e) {
				System.out.println(e);
			}
			writers.put(aType, writer);
		}
		return writer;
		
	}
}